const passwordValid = (rule, value, callback) => {
  if (!/^(?![^a-zA-Z]+$)(?!\D+$)/.test(value)) {
    callback(new Error('6-18位字母、数字'))
  } else {
    callback()
  }
}

// 手机
const phone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('手机号不能未空'))
  } else {
    if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value)) {
      callback(new Error('请填写正确的手机号'))
    } else {
      callback()
    }
  }
}
// 身份证
const idCard = (rule, value, callback) => {
  if (!value) {
    callback(new Error('身份证不能未空'))
  } else {
    if (
      !/^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/.test(
        value
      )
    ) {
      callback(new Error('请填写正确的身份证'))
    } else {
      callback()
    }
  }
}
// 邮箱
const email = (rule, value, callback) => {
  if (!value) {
    callback(new Error('邮箱不能未空'))
  } else {
    if (
      !/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
        value
      )
    ) {
      callback(new Error('请填写正确的邮箱'))
    } else {
      callback()
    }
  }
}
// 金额 非必填
const notRMoney = (rule, value, callback, msg) => {
  if (!value) {
    callback()
  }
  if (/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/.test(value)) {
    callback()
  } else {
    callback(new Error(`请填写正确的值`))
  }
}
// 金额必填
const notNullMoney = (rule, value, callback, msg) => {
  if (value === '' || value === undefined || value === null) {
    callback(new Error(`该字段不能为空`))
  }
  if (/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/.test(value)) {
    callback()
  } else {
    callback(new Error(`请填写正确的值`))
  }
}
// 多行input校验
const manyInput = (rule, value, callback, msg) => {
  if (value.every((m) => m !== '')) {
    callback()
  } else {
    callback(new Error(`值不能未空`))
  }
}
const objectIsNotEmpty = (rule, value, callback, msg) => {
  if (value) {
    let keyList = Object.values(value)
    if (keyList.every((m) => m)) {
      callback()
    } else {
      callback(new Error(`值不能未空`))
    }
  } else {
    callback(new Error(`值不能未空`))
  }
}
const arrIsNotEmpty = (rule, value, callback, msg) => {
  if (value && value.length !== 0) {
    callback()
  } else {
    callback(new Error(`值不能未空`))
  }
}
const formRules = {
  必填: [{ required: true, message: '该字段不能为空', trigger: 'blur' }],
  必填change: [{ required: true, message: '该字段不能为空', trigger: 'change' }],
  金额必填: [{ required: true, validator: notNullMoney, trigger: 'blur' }],
  金额非必填: [{ required: false, validator: notRMoney, trigger: 'blur' }],
  密码: [{ required: true, validator: passwordValid, trigger: 'blur' }],
  手机: [{ required: true, validator: phone, trigger: 'blur' }],
  邮箱: [{ required: true, validator: email, trigger: 'blur' }],
  身份证: [{ required: true, validator: idCard, trigger: 'blur' }],
  多输入必填: [{ required: true, validator: manyInput, trigger: 'blur' }],
  对象不为空: [{ required: true, validator: objectIsNotEmpty, trigger: 'blur' }],
  数组必填: [{ required: true, validator: arrIsNotEmpty, trigger: 'blur' }]
}

export { formRules }
